<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="退款单号" prop="refundNo">
        <el-input v-model="queryParams.refundNo" placeholder="请输入退款单号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter="handleQuery" />
      </el-form-item>

      <el-form-item label="退款时间">
        <el-date-picker v-model="daterangeRefundTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item label="退款状态" prop="refundState">
        <el-select v-model="queryParams.refundState" placeholder="请选择退款状态" clearable>
          <el-option v-for="dict in refundOptions" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="创建人" prop="createBy">
        <el-input v-model="queryParams.createBy" placeholder="请输入创建人" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="daterangeCreateTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['admin:refundOrder:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['admin:refundOrder:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="refundOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="退款单号" align="center" prop="refundNo" />
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="订单金额" align="center" prop="orderPrice" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column label="退款状态" align="center" prop="refundState">
        <template #default="scope">
          <span>{{ stateFun(scope.row.refundState, refundOptions) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:refundOrder:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['admin:refundOrder:remove']">删除</el-button> -->

          <el-button link type="primary" icon="Edit" @click="handleRefund(scope.row)"
            v-if="scope.row.refundState == 0">退款审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 退款审核 -->
    <el-dialog title="退款审核" v-model="refundShow" width="500px" append-to-body>
      <el-form ref="refundOrderRef" :model="refundForm" label-width="80px">
        <el-form-item label="审核状态" prop="refundState">
          <el-radio-group v-model="refundForm.refundState">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="2">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleRefundConfirm">确 定</el-button>
          <el-button @click="refundShow = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RefundOrder">
import { listRefundOrder, getRefundOrder, delRefundOrder, addRefundOrder, updateRefundOrder, auditRefundOrder } from "@/api/admin/refundOrder";
import { ElLoading } from 'element-plus'
const { proxy } = getCurrentInstance();

const refundOrderList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeRefundTime = ref([]);
const daterangeCreateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    refundNo: null,
    orderNo: null,
    refundTradeNo: null,
    refundTime: null,
    refundPrice: null,
    refundState: null,
    refundRemark: null,
    createBy: null,
    createTime: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

const refundOptions = ref([
  {
    label: "审核中",
    value: 0
  },
  {
    label: "退款成功",
    value: 1
  },
  {
    label: "退款失败",
    value: 2
  }
])

/**
 * 退款审核
 */
const refundShow = ref(false);
const refundForm = ref({
  refundNo: null,
  refundState: null,
})
function handleRefund(row) {
  refundForm.value = {
    refundNo: row.refundNo,
    refundState: row.refundState
  }
  refundShow.value = true;
}
function handleRefundConfirm() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  auditRefundOrder(refundForm.value).then((res) => {
    proxy.$modal.msgSuccess("操作成功");
    loading.close()
    refundForm.value = false;
    getList();
  });
}

/** 查询订单退款列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeRefundTime && '' != daterangeRefundTime) {
    queryParams.value.params["beginRefundTime"] = daterangeRefundTime.value[0];
    queryParams.value.params["endRefundTime"] = daterangeRefundTime.value[1];
  }
  if (null != daterangeCreateTime && '' != daterangeCreateTime) {
    queryParams.value.params["beginCreateTime"] = daterangeCreateTime.value[0];
    queryParams.value.params["endCreateTime"] = daterangeCreateTime.value[1];
  }
  listRefundOrder(queryParams.value).then(response => {
    refundOrderList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    refundNo: null,
    orderNo: null,
    orderPrice: null,
    orderType: null,
    refundTradeNo: null,
    refundTime: null,
    refundPrice: null,
    refundState: null,
    refundRemark: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    createTime: null
  };
  proxy.resetForm("refundOrderRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeRefundTime.value = [];
  daterangeCreateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.refundNo);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加订单退款";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _refundNo = row.refundNo || ids.value
  getRefundOrder(_refundNo).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改订单退款";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["refundOrderRef"].validate(valid => {
    if (valid) {
      if (form.value.refundNo != null) {
        updateRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addRefundOrder(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _refundNos = row.refundNo || ids.value;
  proxy.$modal.confirm('是否确认删除订单退款编号为"' + _refundNos + '"的数据项？').then(function () {
    return delRefundOrder(_refundNos);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('admin/refundOrder/export', {
    ...queryParams.value
  }, `refundOrder_${new Date().getTime()}.xlsx`)
}

getList();
</script>
